package com.mlnx.work.utils;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by amanda.shan on 2020/6/12.
 */
public class UserIdUtils {

    public static List<String> existIds = new ArrayList<>();

    private static String[] arrays = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",};

    public static String getNumbId(int len) {
        Random random = new Random();
        while (true) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < len; i++) {
                if (i == 0) {
                    while (true) {
                        String s = arrays[random.nextInt(10)];
                        if (i == 0 && s.equals("0")) {
                            continue;
                        }else{
                            builder.append(s);
                            break;
                        }
                    }
                } else {
                    builder.append(arrays[random.nextInt(10)]);
                }
            }

            boolean exist = false;
            String id = builder.toString();
            for (String s : existIds) {
                if (id.equals(s)) {
                    exist = true;
                    break;
                }
            }

            if (!exist) {
                existIds.add(id);
                return id;
            }
        }
    }

    public static void main(String[] args) {

        System.out.println(IdWorker.getId());
        for (int i = 0; i < 100000; i++) {
            System.out.println(getNumbId(8));
            System.out.println(UserIdUtils.existIds.size());
        }
    }
}
